package com.bwie.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesUtil {


    private static String KEY = "1234567890123456"; // 密钥，长度必须是 16
    private static String IV = "qwertyuiopasdfgh"; // 偏移量，长度必须是 16



    /**
     * 加密返回的数据转换成 String 类型
     *
     * @param content 明文
     * @return
     * @throws Exception
     */
    public static String encrypt(String content) throws Exception {
        if(StringUtils.isEmpty(content)){//空明文不加密
            return content;
        }
        return base64ToString(AES_CBC_Encrypt(content.getBytes("UTF-8"), KEY.getBytes(), IV.getBytes()));
    }

    /**
     * 将解密返回的数据转换成 String 类型
     *
     * @param content Base64编码的密文
     * @return
     * @throws Exception
     */
    public static String decrypt(String content) throws Exception {
        if(StringUtils.isEmpty(content)){//空密文不解密
            return content;
        }
        return new String(AES_CBC_Decrypt(stringToBase64(content), KEY.getBytes(), IV.getBytes()));
    }



    private static byte[] AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    private static byte[] AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    /**
     * 字符串装换成 Base64
     */
    public static byte[] stringToBase64(String key) throws Exception {
        return Base64.decodeBase64(key.getBytes());
    }

    /**
     * Base64装换成字符串
     */
    public static String base64ToString(byte[] key) throws Exception {
        return new Base64().encodeToString(key);
    }

    public static void main(String[] args) {


        try {
            //加密        6z7ltT5jzM8Y7yzixO2p0g==
            String encryptStr = encrypt("123456");
            System.out.println("加密后的结果："+encryptStr);


             //解密
            String decryptStr = decrypt("lpR7vT0bM/LyjDR+EEQ7uA==");

            System.out.println("解密后的结果是："+decryptStr);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}

